package matrix

import C "gitee.com/ljfirst/algo-go-sdk/common/constant"

/**
 * @author ljfirst
 * @version V1.0
 * @date 2023/8/1 21:02
 * @author-Email ljfirst@mail.ustc.edu.cn
 * @blogURL https://blog.csdn.net/ljfirst
 * @description 矩阵行列式
 **/
type Determinant struct {
}

func (m *Determinant) CalMethod(matrix [][]int) int {
	if len(matrix) == 1 {
		return matrix[0][0]
	}
	result, flagC := 0, 1
	for i := 0; i < len(matrix); i++ {
		f := 0
		nextMatrix := make([][]int, len(matrix)-1)
		for j := 0; j < len(matrix); j++ {
			if i == j {
				f = -1
				continue
			}
			nextMatrix[j+f] = make([]int, len(matrix)-1)
			c := 0
			for l := 0; l < len(matrix); l++ {
				if l == 0 {
					c = -1
					continue
				}
				nextMatrix[j+f][l+c] = matrix[j][l]
			}
		}
		result += flagC * matrix[i][0] * m.CalMethod(nextMatrix)
		flagC = -1 * flagC
	}
	return result
}

func (m *Determinant) GetAttribute() *C.Attribute {
	return &C.Attribute{
		Tags: []string{},
		Desc: &C.Desc{
			Name:        "Determinant",
			NameCn:      "矩阵行列式",
			Description: "",
			ParamsDesc: map[string]string{
			},
			Example: map[int]string{
			},
		},
	}
}
